package com.yuqiang.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yuqiang.common.model.ResultModel;
import com.yuqiang.user.model.bo.UserInfoBo;
import com.yuqiang.user.model.vo.UserInfoVo;
import com.yuqiang.user.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
	
	@Autowired
	private UserInfoService userInfoService;
	
	protected Map<String, Object> resultMap = new HashMap<String, Object>();
	
	/**
	 * 会员信息管理程序入口
	 * @return
	 */
	@RequestMapping("/initUserInfo")
	public String initResourceConfig(HttpServletRequest request) {
		if(request.getSession().getAttribute("userName")==null){
			return "/index/login";
		}
		return "/user/UserInfoManage";
	}
	
	/**
	 * 个人信息管理程序入口
	 * @return
	 */
	@RequestMapping("/initUserPersonal")
	public ModelAndView initUserPersonal(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/user/UserPersonalInfo");
		UserInfoBo userInfoBo = new UserInfoBo();
		if(request.getSession().getAttribute("userName")==null){
			modelAndView.setViewName("/index/login");
			return modelAndView;
		}
		userInfoBo.setUserId(Integer.parseInt(request.getSession().getAttribute("userId").toString()));
		List<UserInfoVo> listUserInfoVo = userInfoService
				.selectUserInfoRows(userInfoBo);
		if(CollectionUtils.isNotEmpty(listUserInfoVo)){
			UserInfoVo user = listUserInfoVo.get(0);
			modelAndView.addObject("user",user);
		}
		return modelAndView;
	}

	/**
	 * 获取用户信息
	 * @return
	 */
	@RequestMapping("/queryUserInfoList")
	@ResponseBody
	public Map<String, Object> queryUserInfoList(
			@ModelAttribute UserInfoBo userInfoBo,HttpServletRequest request) {
		if(request.getSession().getAttribute("userName")==null){
			resultMap.put("rows", null);
			resultMap.put("total", 0);
			return resultMap;
		}
		if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
			resultMap.put("rows", null);
			resultMap.put("total", 0);
			return resultMap;
		}
		List<UserInfoVo> listUserInfoVo = userInfoService
				.selectUserInfoRows(userInfoBo);
		resultMap.put("rows", listUserInfoVo);
		resultMap.put("total", userInfoService.selectUserInfoTotal(userInfoBo));
		return resultMap;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/queryUserInfo")
	@ResponseBody
	public UserInfoVo queryUserInfo(
			@ModelAttribute UserInfoBo userInfoBo,HttpServletRequest request) {
		if(request.getSession().getAttribute("userName")==null){
			return null;
		}
		if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
			return null;
		}
		List<UserInfoVo> listUserInfoVo = userInfoService
				.selectUserInfoRows(userInfoBo);
		if(CollectionUtils.isNotEmpty(listUserInfoVo)){
			return listUserInfoVo.get(0);
		}
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/saveUserInfo")
	@ResponseBody
	public ResultModel saveUserInfo(
			@ModelAttribute UserInfoBo userInfoBo,HttpServletRequest request) {

		ResultModel resultModel = new ResultModel();
		if(request.getSession().getAttribute("userName")==null){
			resultModel.setSuccess(false);
			resultModel.setMessage("请登录后再操作！");
			return resultModel;
		}
		if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
			resultModel.setSuccess(false);
			resultModel.setMessage("你不是管理员！");
			return resultModel;
		}
		userInfoBo.setUserLastmodifiedby(request.getSession().getAttribute("userName").toString());
		userInfoService.updateUserInfoById(userInfoBo);
		return resultModel;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/openOrCloseUserInfo")
	@ResponseBody
	public ResultModel openOrCloseUserInfo(
			@ModelAttribute UserInfoBo userInfoBo,HttpServletRequest request) {
		ResultModel resultModel = new ResultModel();
		if(request.getSession().getAttribute("userName")==null){
			resultModel.setSuccess(false);
			resultModel.setMessage("请登录后再操作！");
			return resultModel;
		}
		if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
			resultModel.setSuccess(false);
			resultModel.setMessage("你不是管理员！");
			return resultModel;
		}
		userInfoBo.setUserLastmodifiedby(request.getSession().getAttribute("userName").toString());
		userInfoService.updateUserInfoById(userInfoBo);
		return resultModel;
	}
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/saveUserPersonalInfo")
	@ResponseBody
	public ResultModel saveUserPersonalInfo(UserInfoBo userInfoBo,HttpServletRequest request) {
		ResultModel resultModel = new ResultModel();
		if(request.getSession().getAttribute("userName")==null){
			resultModel.setSuccess(false);
			resultModel.setMessage("请登录后再操作！");
			return resultModel;
		}
		if(StringUtils.isNotBlank(userInfoBo.getUserPassword())){
			if(userInfoService.checkPassword(userInfoBo)>0){
				userInfoBo.setUserPassword(userInfoBo.getUserNewPassword());
				userInfoService.updateUserInfoById(userInfoBo);
				return resultModel;
			}else{
				userInfoBo.setUserPassword(null);
				userInfoService.updateUserInfoById(userInfoBo);
				resultModel.setSuccess(false);
				resultModel.setMessage("密码错误，信息修改成功！密码修改失败！");
				return resultModel;
			}
		}else{
			userInfoBo.setUserLastmodifiedby(request.getSession().getAttribute("userName").toString());
			userInfoService.updateUserInfoById(userInfoBo);
			return resultModel;
		}
		
	}	
}
